<template>
	<a-layout-header
		:class="['header', { 'header-scrolled': isScrolled }]"
		:style="{
			background: isScrolled ? 'rgba(255, 255, 255, 0.95)' : 'transparent',
			backdropFilter: isScrolled ? 'blur(10px)' : 'none',
			boxShadow: isScrolled ? '0 2px 8px rgba(0, 0, 0, 0.06)' : 'none',
			transition: 'all 0.3s ease'
		}"
	>
		<div class="header-content">
			<!-- 左侧 logo，调整靠左对齐 -->
			<div class="logo" @click="scrollToTop">
				<img :src="logoUrl" alt="Logo" />
				<div class="logo-text-wrapper">
					<div class="logo-text">特创易</div>
				</div>
			</div>

			<!-- 导航菜单，整体向右移动60px -->
			<div class="menu-container">
				<a-menu v-model:selectedKeys="selectedKeys" mode="horizontal" class="nav-menu">
					<!-- 设计服务下拉菜单 -->
					<a-sub-menu key="design" title="设计服务">
						<a-menu-item v-for="item in designServices" :key="item.key">
							{{ item.title }}
						</a-menu-item>
					</a-sub-menu>

					<!-- 项目案例下拉菜单，进一步增加间距 -->
					<a-sub-menu key="cases" title="项目案例">
						<div class="cases-dropdown">
							<!-- 按服务 -->
							<div class="category-column">
								<h4>按服务</h4>
								<div class="category-items">
									<a
										v-for="item in designServices"
										:key="item.key"
										:class="['category-item', { updating: item.updating }]"
									>
										{{ item.title }}
									</a>
								</div>
							</div>

							<!-- 按行业 -->
							<div class="category-column">
								<div class="category-header">
									<h4>按行业</h4>
									<a class="view-all">全部行业 ></a>
								</div>
								<div class="category-items">
									<a v-for="item in caseCategories.byIndustry.items" :key="item.key" class="category-item">
										{{ item.title }}
									</a>
								</div>
							</div>

							<!-- 按产品 -->
							<div class="category-column">
								<div class="category-header">
									<h4>按产品</h4>
								</div>
								<div class="category-items">
									<a v-for="item in caseCategories.byProduct.items" :key="item.key" class="category-item">
										{{ item.title }}
									</a>
								</div>
							</div>

							<!-- 店铺品牌 -->
							<div class="category-column">
								<div class="category-header">
									<h4>店铺品牌</h4>
								</div>
								<div class="category-items">
									<a v-for="item in caseCategories.byShop.items" :key="item.key" class="category-item">
										{{ item.title }}
									</a>
								</div>
							</div>

							<!-- 按城市 -->
							<div class="category-column">
								<div class="category-header">
									<h4>按城市</h4>
									<a class="view-all">全部城市 ></a>
								</div>
								<div class="category-items city-items">
									<a v-for="item in caseCategories.byCity.items" :key="item.key" class="category-item">
										{{ item.title }}
									</a>
								</div>
							</div>
						</div>
					</a-sub-menu>

					<a-menu-item key="appointment" @click="goToReservation">预约设计</a-menu-item>
					<a-menu-item key="taskhall">任务大厅</a-menu-item>
					<a-menu-item key="findDesigner">找设计</a-menu-item>
					<a-menu-item key="creativePeople">创意人入驻</a-menu-item>
					<a-menu-item key="other">其他</a-menu-item>
				</a-menu>
			</div>

			<!-- 右侧登录注册按钮，确保靠右对齐 -->
			<div class="right">
				<!-- 未登录状态 -->
				<template v-if="!isLoggedIn">
					<a-button type="link" @click="openLoginPage">登录</a-button>
					<a-button type="primary">注册</a-button>
				</template>
				
				<!-- 已登录状态 -->
				<template v-else>
					<a-dropdown>
						<a class="user-dropdown-link" @click.prevent>
							<a-avatar :src="userStore.avatar || defaultAvatar" :size="32" />
							<span class="username">{{ userStore.roleName }} <span class="dot"></span> {{ userStore.nickname || userStore.name || '用户' }}</span>
							<down-outlined />
						</a>
						<template #overlay>
							<a-menu>
								<a-menu-item key="profile">
									<user-outlined />
									个人中心
								</a-menu-item>
								<a-menu-item key="orders">
									<shopping-outlined />
									我的订单
								</a-menu-item>
								<a-menu-divider />
								<a-menu-item key="logout" @click="handleLogout">
									<logout-outlined />
									退出登录
								</a-menu-item>
							</a-menu>
						</template>
					</a-dropdown>
				</template>
			</div>
		</div>
	</a-layout-header>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, onUnmounted, computed } from 'vue';
import { useRouter } from 'vue-router';
import { useUserStore } from '@/store/user';
import { 
	DownOutlined, 
	UserOutlined, 
	ShoppingOutlined, 
	LogoutOutlined 
} from '@ant-design/icons-vue';

// 导入类型和数据
interface DesignServiceItem {
	key: string;
	title: string;
	updating?: boolean;
}

interface CategoryItem {
	key: string;
	title: string;
}

interface CaseCategories {
	byIndustry: {
		items: CategoryItem[];
	};
	byProduct: {
		items: CategoryItem[];
	};
	byShop: {
		items: CategoryItem[];
	};
	byCity: {
		items: CategoryItem[];
	};
}

export default defineComponent({
	name: 'AppHeader',
	components: {
		DownOutlined,
		UserOutlined,
		ShoppingOutlined,
		LogoutOutlined
	},
	setup() {
		const router = useRouter();
		const userStore = useUserStore();
		const selectedKeys = ref<string[]>(['首页']);
		const isScrolled = ref<boolean>(false);
		
		// 默认头像
		const defaultAvatar = 'https://api.ixiaowai.cn/mcapi/mcapi.php';
		
		// 判断用户是否登录
		const isLoggedIn = computed(() => {
			return userStore.token && userStore.name;
		});

		// Logo URL
		const logoUrl = ref('/src/assets/images/home/logo.png'); // 请确保路径正确

		// 设计服务数据
		const designServices = ref<DesignServiceItem[]>([
			{ key: 'logo', title: 'LOGO设计', updating: true },
			{ key: 'vi', title: '品牌VI设计' },
			{ key: 'web', title: '网站设计' },
			{ key: 'app', title: 'APP设计' },
			{ key: 'package', title: '包装设计' },
			{ key: 'poster', title: '海报设计' },
			{ key: 'brochure', title: '宣传册设计' },
			{ key: 'illustration', title: '插画设计' }
		]);

		// 案例分类数据
		const caseCategories = ref<CaseCategories>({
			byIndustry: {
				items: [
					{ key: 'tech', title: '科技' },
					{ key: 'finance', title: '金融' },
					{ key: 'education', title: '教育' },
					{ key: 'health', title: '医疗健康' },
					{ key: 'food', title: '餐饮美食' },
					{ key: 'fashion', title: '时尚美妆' },
					{ key: 'real-estate', title: '房地产' },
					{ key: 'tourism', title: '旅游酒店' }
				]
			},
			byProduct: {
				items: [
					{ key: 'digital', title: '数码电子' },
					{ key: 'clothing', title: '服装鞋帽' },
					{ key: 'furniture', title: '家居家具' },
					{ key: 'maternal', title: '母婴用品' },
					{ key: 'sports', title: '运动户外' },
					{ key: 'food-product', title: '食品饮料' },
					{ key: 'cosmetics', title: '化妆品' },
					{ key: 'jewelry', title: '珠宝首饰' }
				]
			},
			byShop: {
				items: [
					{ key: 'retail', title: '零售店' },
					{ key: 'restaurant', title: '餐厅' },
					{ key: 'cafe', title: '咖啡厅' },
					{ key: 'bakery', title: '烘焙店' },
					{ key: 'salon', title: '美发沙龙' },
					{ key: 'spa', title: '水疗会所' },
					{ key: 'gym', title: '健身房' },
					{ key: 'hotel', title: '酒店' }
				]
			},
			byCity: {
				items: [
					{ key: 'beijing', title: '北京' },
					{ key: 'shanghai', title: '上海' },
					{ key: 'guangzhou', title: '广州' },
					{ key: 'shenzhen', title: '深圳' },
					{ key: 'hangzhou', title: '杭州' },
					{ key: 'chengdu', title: '成都' },
					{ key: 'nanjing', title: '南京' },
					{ key: 'wuhan', title: '武汉' },
					{ key: 'tianjin', title: '天津' },
					{ key: 'chongqing', title: '重庆' },
					{ key: 'xian', title: '西安' },
					{ key: 'suzhou', title: '苏州' }
				]
			}
		});

		// 监听滚动事件
		const handleScroll = () => {
			isScrolled.value = window.scrollY > 50;
		};

		onMounted(() => {
			window.addEventListener('scroll', handleScroll);
			
			// 如果已登录但没有用户信息，则获取用户信息
			if (isLoggedIn.value && !userStore.name) {
				userStore.getInfo().catch(error => {
					console.error('获取用户信息失败:', error);
				});
			}
		});

		onUnmounted(() => {
			window.removeEventListener('scroll', handleScroll);
		});

		// 回到顶部
		const scrollToTop = () => {
			window.scrollTo({
				top: 0,
				behavior: 'smooth'
			});
			router.push('/');
		};

		// 打开登录页面
		const openLoginPage = () => {
			router.push('/login');
		};
		
		// 退出登录
		const handleLogout = async () => {
			try {
				await userStore.logout();
				router.push('/login');
			} catch (error) {
				console.error('登出失败:', error);
			}
		};

		// 跳转到预约页面
		const goToReservation = () => {
			router.push('/reservation');
		};

		return {
			selectedKeys,
			isScrolled,
			logoUrl,
			designServices,
			caseCategories,
			scrollToTop,
			openLoginPage,
			goToReservation,
			isLoggedIn,
			userStore,
			defaultAvatar,
			handleLogout
		};
	}
});
</script>

<style lang="scss" scoped>
@import '@/styles/mixins.scss';

// 定义响应式 mixin
@mixin respond-to($breakpoint) {
	@media (max-width: $breakpoint) {
		@content;
	}
}

.header {
	position: fixed;
	z-index: 1000;
	width: 100%;
	padding: 0;
	background: transparent;
	transition: all 0.3s ease;

	.header-content {
		display: flex;
		align-items: center;
		width: 100%;
		height: 64px;
		padding: 0 24px;
	}

	// logo 部分
	.logo {
		display: flex;
		align-items: center;
		gap: 8px;
		cursor: pointer;
		margin-right: 100px; // 保持右侧间距

		img {
			width: 30px;
			height: 30px;
		}

		.logo-text-wrapper {
			position: relative;
			display: flex;
			align-items: center;
			height: 30px;
		}
	}

	// 导航菜单部分，整体向右移动60px
	.menu-container {
		display: flex;
		flex: 0 0 auto;
		justify-content: flex-start;
		min-width: 0;
		margin-left: 60px; // 保持向右移动60px

		.nav-menu {
			display: flex;
			background: transparent;
			border-bottom: none;

			// 调整菜单项间距
			.ant-menu-item,
			.ant-menu-submenu {
				padding: 0 15px; // 减小内边距
				margin-right: 0; // 移除右侧外边距
			}

			// 特别调整"设计服务"菜单项
			.ant-menu-submenu-title:has(+ .ant-menu-submenu[title='项目案例']) {
				margin-right: -10px; // 使"设计服务"向"项目案例"靠近10px
			}

			// 或者使用类选择器（如果上面的选择器不生效）
			.ant-menu-submenu:first-child {
				margin-right: -10px; // 使"设计服务"向"项目案例"靠近10px
			}
		}
	}

	// 右侧部分
	.right {
		display: flex;
		gap: 12px;
		align-items: center;
		margin-left: auto;
	}
}

// 下拉菜单样式，增加高度
.cases-dropdown {
	display: flex;
	padding: 40px; // 保持内边距
	background: white;
	border-radius: 4px;
	box-shadow: 0 2px 8px rgb(0 0 0 / 15%);
	min-width: 1000px; // 保持宽度

	.category-column {
		flex: 1;
		padding: 0 30px; // 保持列间距
		border-right: 1px solid #f0f0f0;

		&:last-child {
			border-right: none;
		}

		h4 {
			font-size: 18px; // 增大标题字体
			font-weight: 500;
			color: #333;
			margin-bottom: 30px; // 增加标题下方间距
		}

		.category-header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 30px; // 增加标题下方间距

			.view-all {
				font-size: 12px;
				color: #1890ff;
				cursor: pointer;
			}
		}

		.category-items {
			display: flex;
			flex-direction: column;
			gap: 24px; // 增加项目间距

			&.city-items {
				display: grid;
				grid-template-columns: repeat(2, 1fr);
				gap: 24px; // 增加城市项目间距
			}

			.category-item {
				font-size: 15px; // 增大字体
				line-height: 1.8; // 增加行高
				color: #666;
				text-decoration: none;
				transition: color 0.3s;

				&:hover {
					color: #1890ff;
				}

				&.updating::after {
					content: '更新中';
					margin-left: 4px;
					font-size: 12px;
					color: #ff4d4f;
				}
			}
		}
	}
}

// 特创易文字样式保持不变
.logo-text {
	position: relative;
	display: inline-block;
	font-family: 'Microsoft YaHei', '微软雅黑', sans-serif;
	font-size: 24px;
	font-weight: bold;
	text-shadow: 0 0 1px rgb(255 255 255 / 20%);
	letter-spacing: 1px;
	white-space: nowrap;
	background-image: linear-gradient(135deg, #0070e0 0%, #6a11cb 100%);
	background-clip: text;
	transition: transform 0.3s;
	transform-origin: center;
	-webkit-text-fill-color: transparent;

	// 增强3D效果和清晰度
	&::before {
		position: absolute;
		top: 0;
		left: 0;
		z-index: -1;
		color: transparent;
		content: '特创易';
		-webkit-text-stroke: 1.5px rgb(0 112 224 / 40%);
	}

	// 添加底部装饰线
	&::after {
		position: absolute;
		bottom: -2px;
		left: 0;
		width: 100%;
		height: 2px;
		content: '';
		background: linear-gradient(90deg, transparent, rgb(106 17 203 / 70%), transparent);
		opacity: 0.8;
		transition: all 0.3s;
		transform: scaleX(0.7);
	}

	&:hover {
		transform: scale(1.05);

		&::after {
			opacity: 1;
			transform: scaleX(1);
		}
	}
}

// 响应式适配
@include respond-to(768px) {
	.header {
		.menu-container {
			display: none;
		}
	}
}

.user-dropdown-link {
	display: flex;
	align-items: center;
	gap: 8px;
	color: #333;
	
	.username {
		font-size: 14px;
		margin-left: 5px;
		display: flex;
		align-items: center;
		
		.dot {
			display: inline-block;
			width: 5px;
			height: 5px;
			border-radius: 50%;
			background-color: #666;
			margin: 0 5px;
		}
	}
}
</style>
